# -*- encoding:utf-8 -*-
# @Time : 2020/12/07 10:00
# @Author : ZHH
"""
1.快速统计:
快速统计字符串每个元素出现的次数： 如"abcevaefegsgdghfdefavbdr"， “a”:x,"b":y
2.降序切割:
快速统计字符串每个元素出现的次数： 如"abcevaefegsgdghfdefavbdr"， “a”:x,"b":y，根据出现的次数降序排列，并且列出出现次数最多的三个元素
"""
from collections import defaultdict


def count_cal(string: str) -> dict:
    if not string:
        pass
    __dict = {}
    for item in string:
        __dict[item] = __dict.get(item, 0) + 1
    return __dict


def count_cal_sort(string: str) -> dict:
    if not string:
        pass
    __3th = []
    __dict = defaultdict(int)
    for item in string:
        __dict[item] += 1
    sorted_dict = sorted(__dict.items(), key=lambda x: x[1], reverse=True)
    for i in range(3):
        __3th.append(sorted_dict[i][0])
    return {"倒叙排列字典": sorted_dict, "出现最多的三个字母": __3th}


print(count_cal_sort("abcevaefegsgdghfdefavbdr"))
#用普通字典试试